package com.example.demo.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重入锁演示,不同的方法，多次上锁，应用场景是啥呢？
 * @author cici
 */
@Slf4j
public class ReentrantLock1 {

    private static ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) {
        lock.lock();
        try {
            log.info("main");
            method1();
        }finally {
            lock.unlock();
        }
    }

    public static void method1(){
        lock.lock();
        try {
            log.info("method1");
            method2();
        }finally {
            lock.unlock();
        }
    }

    public static void method2(){
        lock.lock();
        try {
            log.info("method2");
        }finally {
            lock.unlock();
        }
    }

}
